package com.zhinong.controller.user;

import com.zhinong.entity.Product;
import com.zhinong.entity.Products;
import com.zhinong.result.Result;
import com.zhinong.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/search")
@CrossOrigin(origins = "http://localhost:8080")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/hotTags")
    public Result<List<String>> getHotTags() {
        List<String> hotTags = searchService.getHotTags();
        return Result.success(hotTags);
    }

    @GetMapping("/history")
    public Result<List<String>> getSearchHistory(@RequestParam String userId) {
        List<String> history = searchService.getSearchHistory(userId);
        return Result.success(history);
    }

    @PostMapping("/addHistory")
    public Result<Void> addSearchHistory(
            @RequestParam String userId,
            @RequestParam String keyword) {
        searchService.addSearchHistory(userId, keyword);
        return Result.success();
    }

    @GetMapping("/products")
    public Result<List<Products>> searchProducts(@RequestParam String keyword) {
        List<Products> products = searchService.searchProducts(keyword);
        return Result.success(products);
    }
}